//@原文链接：https://blog.csdn.net/ITSOK_123/article/details/124006640

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
 //迭代解法
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dhead = new ListNode(0,head);
        ListNode* cur  = dhead;
        while(cur->next != nullptr &&cur->next->next!=nullptr){
            ListNode* curn = cur->next;
            ListNode* curnn = curn->next;
            ListNode* curnnn = curnn->next;
            cur->next= curnn;
            cur->next->next = curn;
            cur->next->next->next = curnnn;

            cur = cur->next->next;
        }
        return dhead->next;
    }
};

//递归解法
ListNode* swapPairs(ListNode* head) {
        if(head == nullptr || head->next == nullptr){
            return head;
        }
        ListNode* newHead = head->next;
        head->next = swapPairs(newHead->next);
        newHead->next = head;
        return newHead;
    }
